var optionType = "";
var setting = {
	view: {
		selectedMulti: false
	},
	edit: {
		enable: true,
		showRemoveBtn: false,
		showRenameBtn: false
	},
	data: {
		keep: {
			parent: true,
			leaf: true
		},
		simpleData: {
			enable: true
		}
	},
	callback: {
//		beforeDrag: beforeDrag,
//		beforeRemove: beforeRemove,
//		beforeRename: beforeRename,
//		onRemove: onRemove,
		onClick: zTreeOnClick,//点击节点函数
	}
};

var zNodes = [
	{	
		pid:0,
		id:1,
		name: "父节点1 - 展开",
		icon_name:"aaa",
		open: true,
		url:"",
		children: [
			{
				pid:1,
				id:11,
				name: "叶子节点111",
				icon_name:"aaa",
				open: true,
				url:""
			},
			{
				pid:1,
				id:12,
				name: "叶子节点112",
				icon_name:"aaa",
				open: true,
				url:""
			},
		]
	}
];

var log, className = "dark";
var newCount = 1;
/*
 * 点击节点回调函数
 * @param treeId {object} 树状菜单事件监听机制对象
 * @param treeNode {string} 树状菜单id值
 * @param node {obj} 所选择的树状菜单项的数据
 */
function zTreeOnClick(treeId, treeNode,node){
	if(node.isParent) return false;
	console.log(treeId, treeNode,node);
	$("#permissTableBox").removeClass("hide");
	$(".menuTitle").text(node.name).attr("data-mid",node.id);
	localStorage.setItem("nodePid",node.pId);
	
	//获取指定菜单下面 的权限列表
	fnAjax.method_4_admin(
		url_join("admin/access/children"),
		{menu_id:node.id},
		"post",
		function(data){
			console.log(data);
			//渲染权限表
			if(data.data.length > 0) createTable(data.data,$("#permissTable"));
				
		}
	);
}
	
//获取指定权限的数据
function getPermiss(aId){
	fnAjax.method_4_admin(
		url_join("admin/access/show"),
		{menu_id:aId},
		"post",
		function(data){
			$("#name").val(data.data.name);
			$("#type").val(data.data.type);
			$("#action").val(data.data.action);
			$("#controller").val(data.data.controller);
			$("#memo").val(data.data.memo);
		}
	);
}



/*
创建表格 
@param data {array} 主要渲染数据
@param containter {object} 主要渲染数据的jq容器
 * */
function createTable(data,containter){
	containter.find("tbody tr").remove();
	$.each(data, function(i,v) {
		var sTr = '<tr class="text-c" data-id="'+ v.id +'" data-mid="'+ v.menu_id +'" data-access_id="'+ v.access_id +'">'+
					'<td class="td-name">'+ v.name +'</td>'+
					'<td class="td-controller">'+
						v.controller +
					'</td>'+
					'<td class="td-action">'+
						v.action +
					'</td>'+
					'<td class="td-type">'+
						v.type +
					'</td>'+
					'<td class="td-memo">'+
						'<span>'+ v.memo +'</span>'+
					'</td>'+
					'<td class="td-option">'+
						'<a class="btn-edit" title="edit">'+
							'<span class="glyphicon glyphicon-edit"></span>编辑'+
						'</a>'+
						'<a class="btn-del ml-10" title="del">'+
							'<span class="glyphicon glyphicon-remove"></span>删除'+
						'</a>'+						
					'</td>'+
				'</tr>';
		containter.append($(sTr));
	});
}


//初始化树状菜单
function initList(){
	fnAjax.method_4_admin(
		url_join("admin/menu/list"),
		"",
		"post",
		function(data){
			console.log(data);
			$.each(data.data, function(i,v) {
				if(v.pid == 0) v.isParent = true;
			});
			$.fn.zTree.init($("#treeDemo"), setting, data.data);
			
		}
	);
}

$(document).ready(function() {
	//初始化树状菜单插件
	initList();
//	$.fn.zTree.init($("#treeDemo"), setting, zNodes);

	//添加权限
	$("body").on("click",".btn-add",function(){
		optionType = "add";
		$("#menuOption").modal("toggle");
		$("#menu_id").val($(".menuTitle").attr("data-mid"));
		$("#aid").removeAttr("name");
	});
	
	//编辑权限
	$("body").on("click",".btn-edit",function(){
		optionType = "edit";
		var aId = $(this).parents("tr").attr("data-id");
		$("#menuOption").modal("toggle");
		$("#menu_id").val($(".menuTitle").attr("data-mid"));//回显权限组id
		$("#aid").val(aId).attr("name","aid");//回显权限id
//		getPermiss(aId);//获取该权限的信息
		
		//回显数据
		$("#name").val($(this).parents("tr").find(".td-name").text());
		$("#controller").val($(this).parents("tr").find(".td-controller").text());
		$("#action").val($(this).parents("tr").find(".td-action").text());
		$("#type").val($(this).parents("tr").find(".td-type").text());
		$("#memo").val($(this).parents("tr").find(".td-memo span").text());
	});
	
	//删除权限
	$("body").on("click",".btn-del",function(){
		var aId = $(this).parents("tr").attr("data-id");
		layer.confirm(
			"确认删除？",
			function(){
				fnAjax.method_4_admin(
					url_join("admin/access/delete"),
					{aid:aId},
					"post",
					function(data){
						console.log(data);
						layer.msg("操作成功",{time:1500},function(){
							window.location.reload();
						});
					}
				);
			}
		);
	});
	
	//初始化表单验证
		$('#form')
			.bootstrapValidator({
				message: '这个值是无效的',
				feedbackIcons: {
					valid: 'glyphicon glyphicon-ok',
					invalid: 'glyphicon glyphicon-remove',
					validating: 'glyphicon glyphicon-refresh'
				},
				fields: {							
					name: {
						message: '名称是无效的',
						validators: {
							notEmpty: {
								message: '名称是必需的，不能是空的'
							},
							stringLength: {
								min: 2,
								max: 20,
								message: '名称必须大于2，小于20个字符'
							},
						}
					},
					memo: {
						validators: {
							notEmpty: {
								message: '不能是空的'
							},	
							
						}
					},
					
				}
			})
			.on('success.form.bv', function(e) {
				// 阻止表单提交
				e.preventDefault();
				// 获得表单实例
				$("#form").append($('<input type="hidden" name="pid" value="'+ localStorage.getItem("nodePid") +'"/>'));
				var $form = $(e.target);
				
				fnAjax.method_4_admin(
					(optionType == "add" ? url_join("admin/access/create") : url_join("admin/access/update")),
					$.serializeToObj($form.serialize()),
//					$form.serialize(),
					"post",
					function(data){
						console.log(data);
						layer.msg("操作成功",{time:1500},function(){
							window.location.reload();
						});
					}
				);
				
			});
		// 手动验证表单
		$('.btn-save').click(function() {
			$('#form').bootstrapValidator('validate');
		});
	
	
});

